//
//  ECAuthorizationTools.m
//  ECAuthorizationTools
//
//  Created by EchoZuo on 2017/6/20.
//  Copyright © 2017年 Echo.Z. All rights reserved.
//

#import "ECAuthorizationTools.h"


@interface ECAuthorizationTools ()<CLLocationManagerDelegate, UINavigationControllerDelegate>

@property (nonatomic,copy) void (^kCLCallBackBlock)(CLAuthorizationStatus state);

@end


@implementation ECAuthorizationTools
#pragma mark -------------------- LocationServices --------------------
#pragma mark -
- (void)checkAndRequestAccessForLocationServicesWithAccessStatus:(AccessForLocationResultBlock)accessStatusCallBack;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL isLocationServicesEnabled = [CLLocationManager locationServicesEnabled];
        if (!isLocationServicesEnabled) {
            ECLog(@"定位服务不可用，例如定位没有打开...");
            [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                           accessStatus:ECLocationAuthorizationStatus_NotSupport
                                                   type:ECPrivacyType_LocationServices];
        }else{
            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
            
            if (status == kCLAuthorizationStatusNotDetermined) {
                self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.delegate = self;
                
                // 两种定位模式：[self.locationManager requestAlwaysAuthorization];
                [self.locationManager requestWhenInUseAuthorization];
                [self setKCLCallBackBlock:^(CLAuthorizationStatus state){
                    if (state == kCLAuthorizationStatusNotDetermined) {
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                       accessStatus:ECLocationAuthorizationStatus_NotDetermined
                                                               type:ECPrivacyType_LocationServices];
                        
                    }else if (state == kCLAuthorizationStatusRestricted) {
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                                       accessStatus:ECLocationAuthorizationStatus_Restricted
                                                                               type:ECPrivacyType_LocationServices];
                        
                    }else if (state == kCLAuthorizationStatusDenied) {
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                                       accessStatus:ECLocationAuthorizationStatus_Restricted
                                                                               type:ECPrivacyType_LocationServices];
                        
                    }else if (state == kCLAuthorizationStatusAuthorizedAlways) {
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                                       accessStatus:ECLocationAuthorizationStatus_AuthorizedAlways
                                                                               type:ECPrivacyType_LocationServices];
                        
                    }else if (state == kCLAuthorizationStatusAuthorizedWhenInUse) {
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                                       accessStatus:ECLocationAuthorizationStatus_AuthorizedWhenInUse
                                                                               type:ECPrivacyType_LocationServices];
                        
                    }else{
                        // kCLAuthorizationStatusAuthorized < ios8
                        [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                                       accessStatus:ECLocationAuthorizationStatus_Authorized
                                                                               type:ECPrivacyType_LocationServices];
                    }
                }];
            }else if (status == kCLAuthorizationStatusRestricted) {
                [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                               accessStatus:ECLocationAuthorizationStatus_Restricted
                                                                       type:ECPrivacyType_LocationServices];
                
            }else if (status == kCLAuthorizationStatusDenied) {
                [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                               accessStatus:ECLocationAuthorizationStatus_Restricted
                                                                       type:ECPrivacyType_LocationServices];
                
            }else if (status == kCLAuthorizationStatusAuthorizedAlways) {
                [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                               accessStatus:ECLocationAuthorizationStatus_AuthorizedAlways
                                                                       type:ECPrivacyType_LocationServices];
                
            }else if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
                [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                               accessStatus:ECLocationAuthorizationStatus_AuthorizedWhenInUse
                                                                       type:ECPrivacyType_LocationServices];
                
            }else{
                // kCLAuthorizationStatusAuthorized < ios8
                [ECAuthorizationTools executeCallBackForForLocationServices:accessStatusCallBack
                                                               accessStatus:ECLocationAuthorizationStatus_Authorized
                                                                       type:ECPrivacyType_LocationServices];
            }
        }
    });
    
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (self.kCLCallBackBlock) {
        self.kCLCallBackBlock(status);
    }
}

#pragma mark -------------------- accessStatus callbacks --------------------
#pragma mark - 
// all CallBack
+ (void)executeCallBack:(AccessForTypeResultBlock)accessStatusCallBack accessStatus:(ECAuthorizationStatus)accessStatus type:(ECPrivacyType)type
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (accessStatusCallBack) {
            accessStatusCallBack(accessStatus, type);
            
            ECLog(@"\n//************************************************************//\n获取权限类型：%@\n权限状态：%@\n//************************************************************//\n",[ECAuthorizationTools stringForPrivacyType:type], [ECAuthorizationTools stringForAuthorizationStatus:accessStatus]);
        }
    });
}

// Location Services CallBack
+ (void)executeCallBackForForLocationServices:(AccessForLocationResultBlock)accessStatusCallBack accessStatus:(ECLocationAuthorizationStatus)accessStatus type:(ECPrivacyType)type
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (accessStatusCallBack) {
            accessStatusCallBack(accessStatus);
            
            ECLog(@"\n//************************************************************//\n获取权限类型：%@\n权限状态：%@\n//************************************************************//\n",[ECAuthorizationTools stringForPrivacyType:type], [ECAuthorizationTools stringForLocationAuthorizationStatus:accessStatus]);
        }
    });
}


#pragma mark -------------------- Public Methods --------------------
#pragma mark -
+ (NSString *)stringForPrivacyType:(ECPrivacyType)privacyType;
{
    if (privacyType == ECPrivacyType_LocationServices) {
        return @"LocationServices";
    }else{
        return @"";
    }
}

+ (NSString *)stringForAuthorizationStatus:(ECAuthorizationStatus)authorizationStatus;
{
    if (authorizationStatus == ECAuthorizationStatus_Authorized) {
        return @"Authorized";
    }else if (authorizationStatus == ECAuthorizationStatus_Denied) {
        return @"Denied";
    }else if (authorizationStatus == ECAuthorizationStatus_Restricted) {
        return @"Restricted";
    }else if (authorizationStatus == ECAuthorizationStatus_NotSupport) {
        return @"NotSupport";
    }else{
        return @"NotDetermined";
    }
}

+ (NSString *)stringForLocationAuthorizationStatus:(ECLocationAuthorizationStatus)locationAuthorizationStatus;
{
    if (locationAuthorizationStatus == ECLocationAuthorizationStatus_Authorized) {
        return @"Location Authorized, < ios8";
    }else if (locationAuthorizationStatus == ECLocationAuthorizationStatus_Denied) {
        return @"Location Denied";
    }else if (locationAuthorizationStatus == ECLocationAuthorizationStatus_Restricted) {
        return @"Location Restricted";
    }else if (locationAuthorizationStatus == ECLocationAuthorizationStatus_NotSupport) {
        return @"Location NotSupport";
    }else if (locationAuthorizationStatus == ECLocationAuthorizationStatus_AuthorizedAlways) {
        return @"Location AuthorizedAlways";
    }else if (locationAuthorizationStatus == ECLocationAuthorizationStatus_AuthorizedWhenInUse) {
        return @"Location AuthorizedWhenInUse";
    }else{
        return @"Location NotDetermined";
    }
}

@end








